%% Author: Administrator
%% Created: 2009-3-20
%% Description: TODO: Add description to ringlord_app
-module(ringlord_app).

-behaviour(application).
-behaviour(supervisor).

%% --------------------------------------------------------------------
%% Include files
%% --------------------------------------------------------------------

%% --------------------------------------------------------------------
%% Behavioural exports
%% --------------------------------------------------------------------
%% application behaviour callback
-export([
	 start/2,
	 stop/1
        ]).

%% supervisor behaviour callback
-export([init/1]).

-export([start/0]).

%% --------------------------------------------------------------------
%% Internal exports
%% --------------------------------------------------------------------
-export([]).

%% --------------------------------------------------------------------
%% Macros
%% --------------------------------------------------------------------

%% --------------------------------------------------------------------
%% Records
%% --------------------------------------------------------------------

%% --------------------------------------------------------------------
%% API Functions
%% --------------------------------------------------------------------


%% ====================================================================!
%% External functions
%% ====================================================================!
start() ->
    application:start(ringlord).

%% --------------------------------------------------------------------
%% Func: start/2
%% Returns: {ok, Pid}        |
%%          {ok, Pid, State} |
%%          {error, Reason}
%% --------------------------------------------------------------------
start(_Type, _StartArgs) ->
    app_util:ensure_base_apps(),
    app_util:init_default_logger("ringlord"),
    
    NodeName = list_to_atom("ringlord@" ++ app_util:get_host_addr_str()),
    net_kernel:start([NodeName, longnames]),
    
    case supervisor:start_link({global,"ringlord_supervisor"}, ringlord_app, []) of
	{ok, Pid} ->
	    {ok, Pid};
	Error ->
	    Error
    end.

%% --------------------------------------------------------------------
%% Func: stop/1
%% Returns: any
%% --------------------------------------------------------------------
stop(_State) ->
    ok.

init([]) ->
    log4erl:debug("the Lord of Ring startup~n"),
    Stragegy = {one_for_one, 10, 10},
    RingLordServer = {
        ring_lord,
        {ring_lord, start_link, []},
        permanent, brutal_kill, worker, [ring_lord]
    },
    
    {ok, {Stragegy, [
                RingLordServer
            ]}
    }.

%% ====================================================================
%% Internal functions
%% ====================================================================
